<?php

namespace app\admin\model\xilujob;

use app\admin\model\User;
use app\common\model\xilujob\CompanyStatistics;
use think\Model;
use traits\model\SoftDelete;

class CompanyProfile extends Model
{

    use SoftDelete;

    

    // 表名
    protected $name = 'xilujob_company_profile';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'personal_audit_status_text',
        'audit_status_text'
    ];

    public static function init() {
//        self::afterDelete(function ($row){
//            if($user=User::where('id', $row->id)->find())
//                $user->delete();
//            if($wxAccount = WxAccount::where('user_id',$row->id)->find())
//                $wxAccount->save('user_id', 0);
//        });
//        self::afterUpdate(function ($row) {
//            $changedData = $row->getChangedData();
//            if(isset($changedData['audit_status'])) {
//                Trainer::where('id',$row->id)->update(['audit_status'=>$changedData['audit_status']]);
//            }
//            if(isset($changedData['personal_audit_status'])) {
//                Trainer::where('id',$row->id)->update(['personal_audit_status'=>$changedData['personal_audit_status']]);
//            }
//        });
        self::afterInsert(function ($row){
            try {
                CompanyStatistics::create(['company_id' => $row->id,'user_id'=>$row->user_id]);
            }catch (\Exception $e) {

            }
        });
    }

    
    public function getPersonalAuditStatusList()
    {
        return ['1' => __('Personal_audit_status 1'), '2' => __('Personal_audit_status 2'), '3' => __('Personal_audit_status 3')];
    }

    public function getAuditStatusList()
    {
        return ['1' => __('Audit_status 1'), '2' => __('Audit_status 2'), '3' => __('Audit_status 3')];
    }


    public function getPersonalAuditStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['personal_audit_status']) ? $data['personal_audit_status'] : '');
        $list = $this->getPersonalAuditStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getAuditStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['audit_status']) ? $data['audit_status'] : '');
        $list = $this->getAuditStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function user() {
        return $this->belongsTo(User::class, 'user_id', 'id',[],'inner')->setEagerlyType(0);
    }

    public function xilujobuser() {
        return $this->belongsTo(XilujobUser::class, 'user_id', 'user_id',[],'inner');
    }

    public function enterpriseinfo() {
        return $this->hasOne('EnterpriseInfo', 'company_id','id',[],'inner')->setEagerlyType(0);
    }

}
